<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="static org.apache.struts2.ServletActionContext.getServletContext" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="com.ld.xuanKe.service.UserService" %>
<%@ page import="com.ld.xuanKe.service.impl.UserServiceImpl" %>
<%@ page import="com.ld.xuanKe.entity.User" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 16:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>用户管理</title>
    <link rel="stylesheet" type="text/css" href="/common.css"/>
</head>
<body>

<%
    ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    UserService userService = (UserServiceImpl) applicationContext.getBean(UserService.class);
    String name = request.getParameter("name");
    List<User> users;
    if (name == null) {
        users = userService.list();
    } else {
        users = userService.getUsersByLikeName(name);
    }
    User user0 = (User) (session.getAttribute("user"));
    String homePath;
    if (user0.getStatus() == 0) {
        homePath = "user";
    } else {
        homePath = "admin";
    }

%>
<a href="/home/<%=homePath%>/index.jsp"
   style="display: inline-block;height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:#DDD;text-decoration: none;">返回个人管理页面</a>
<h3>学生管理说明</h3>
<ul class='explain'>
    <li>学生初始密码为学号</li>
    <li>学生信息删除后,该学生所有的选课信息也会删除,需谨慎</li>
</ul>
<form id='like' action='/home/admin/user.jsp' method='get'><input name='name'>&nbsp&nbsp<input type='submit'
                                                                                                 value='按学生名模糊查询'>
</form>
<div class='navigate'>
    <a href='/home/admin/addUser.jsp'>添加学生</a>
    <a href='/user/downLoad'>导出为excel</a>
    <a href='/user/template'>下载导入excel模板</a>
    <a href='/home/admin/upLoadUserExcel.jsp'>导入excel</a>
</div>
<c:if test="<%=users.isEmpty()%>">
    <h1 style='width: 80%;height:400px;margin: 0 auto;text-align:center;line-height:400px'>暂无更多信息!</h1>
</c:if>
<c:if test="<%=!users.isEmpty()%>">
    <table id='all-request' class='container'>

        <thead>
        <tr>
            <th>学生id</th>
            <th>姓名</th>
            <th>学号</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="<%=users%>" var="user">
            <tr>
                <td>${user.getId() }</td>
                <td>${user.getUsername()}</td>
                <td>${user.getAccount()}</td>
                <td class='accept'><a href="/home/admin/changeUser.jsp?id=${user.getId()}">修改</a></td>
                <td class='accept toDelete'><a class='deleted' href="/user/delete?id=${user.getId()}">删除</a></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</c:if>
</body>
<script>
    let deleted = document.getElementsByClassName("deleted");
    for (let i = 0; i < deleted.length; i++) {
        deleted[i].addEventListener("click", (e) => {
            let b = confirm("你确定要彻底删除吗?");
            if (b == false) {
                e.preventDefault();
            }
        })
    }
</script>
</html>
